package com.ruoyi.seedlingSys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.seedlingSys.domain.Patch;
import com.ruoyi.seedlingSys.domain.Qr;
import com.ruoyi.seedlingSys.domain.vo.PatchVo;
import com.ruoyi.seedlingSys.service.ICaishouService;
import com.ruoyi.seedlingSys.service.IJiagongService;
import com.ruoyi.seedlingSys.service.IPatchService;
import com.ruoyi.seedlingSys.service.IZaipeiService;
import com.ruoyi.seedlingSys.service.IZhongmiaoService;
import com.ruoyi.seedlingSys.service.IZhilijianceService;
import com.ruoyi.seedlingSys.mapper.QrMapper;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 批次管理Controller
 * 
 * @author ruoyi
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/seedlingSys/patch")
public class PatchController extends BaseController {
    @Autowired
    private IPatchService patchService;

    // 在类中注入qrMapper
    @Autowired
    private QrMapper qrMapper;
    @Autowired
    private IZhongmiaoService zhongmiaoService;

    @Autowired
    private IZaipeiService zaipeiService;

    @Autowired
    private IZhilijianceService zhiLiJianCeService;

    @Autowired
    private ICaishouService caishouService;

    @Autowired
    private IJiagongService jiagongService;

    /**
     * 查询批次管理列表
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patch patch) {
        startPage();
        List<Patch> list = patchService.selectPatchList(patch);
        return getDataTable(list);
    }

    /**
     * 导出批次管理列表
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:export')")
    @Log(title = "批次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patch patch) {
        List<Patch> list = patchService.selectPatchList(patch);
        ExcelUtil<Patch> util = new ExcelUtil<Patch>(Patch.class);
        util.exportExcel(response, list, "批次管理数据");
    }

    /**
     * 获取批次管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:query')")
    @GetMapping(value = "/{patchID}")
    public AjaxResult getInfo(@PathVariable("patchID") Long patchID) {
        return success(patchService.selectPatchByPatchID(patchID));
    }

    /**
     * 新增批次管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:add')")
    @Log(title = "批次管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patch patch) {
        return toAjax(patchService.insertPatch(patch));
    }

    /**
     * 修改批次管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:edit')")
    @Log(title = "批次管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patch patch) {
        return toAjax(patchService.updatePatch(patch));
    }

    /**
     * 删除批次管理
     */
    @PreAuthorize("@ss.hasPermi('seedlingSys:patch:remove')")
    @Log(title = "批次管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{patchIDs}")
    public AjaxResult remove(@PathVariable Long[] patchIDs) {
        return toAjax(patchService.deletePatchByPatchIDs(patchIDs));
    }

    /**
     * 通过二维码uuid获取二维码信息，并从中拿到批次ID，然后根据批次ID获取批次信息
     * 要求：批次信息中的育苗记录、栽培记录、采收记录、加工记录、质量检测记录都要有
     * 
     * @param uuid 二维码uuid
     * @return 批次信息
     */
    @Anonymous
    // @Log(title = "通过二维码uuid获取批次信息", businessType = BusinessType.OTHER)
    @GetMapping("/getPatchByQrCode/{uuid}")
    public AjaxResult getPatchByQrCode(@PathVariable String uuid) {
        System.out.println("asdf");
        Patch patch = patchService.getPatchByQrCode(uuid);
        if (patch == null) {
            return error("没有该商品信息");
        }
        // 查询二维码信息
        Qr qr = qrMapper.selectQrByQrUniqueCode(uuid).get(0); // 假设UUID是唯一的，直接取第一个结果
        if (qr == null) {
            return error("没有该二维码信息");
        }
        PatchVo patchVo = new PatchVo();
        patchVo.setPatchID(patch.getPatchID());
        patchVo.setPatchName(patch.getPatchName());
        patchVo.setPatchDescription(patch.getPatchDescription());
        patchVo.setPatchImage(patch.getPatchImage());
        patchVo.setPatchZhongMiaoID(patch.getPatchZhongMiaoID());
        patchVo.setPatchZaiPeiID(patch.getPatchZaiPeiID());
        patchVo.setPatchCaiShouID(patch.getPatchCaiShouID());
        patchVo.setPatchJiaGongID(patch.getPatchJiaGongID());
        patchVo.setPatchZhiLiJianCeID(patch.getPatchZhiLiJianCeID());

        // 填充二维码信息
        patchVo.setQrUniqueCode(qr.getQrUniqueCode());
        patchVo.setQrCustomerName(qr.getQrCustomerName());
        patchVo.setQrCustomerPhone(qr.getQrCustomerPhone());
        patchVo.setQrCustomerAddress(qr.getQrCustomerAddress());
        patchVo.setQrId(qr.getQrID());

        patchVo.setQrRetailerInfo(qr.getQrRetailerInfo());

        // 育苗记录
        if (patch.getPatchZhongMiaoID() != null) {
            patchVo.setZhongmiao(zhongmiaoService.selectZhongmiaoByZhongMiaoID(patch.getPatchZhongMiaoID()));
        }
        // 栽培记录
        if (patch.getPatchZaiPeiID() != null) {
            patchVo.setZaipei(zaipeiService.selectZaipeiByZaiPeiID(patch.getPatchZaiPeiID()));
        }
        // 采收记录
        if (patch.getPatchCaiShouID() != null) {
            patchVo.setCaishou(caishouService.selectCaishouByCaiShouID(patch.getPatchCaiShouID()));
        }
        // 加工记录
        if (patch.getPatchJiaGongID() != null) {
            patchVo.setJiaGong(jiagongService.selectJiagongByJiaGongID(patch.getPatchJiaGongID()));
        }
        // 质量检测记录
        if (patch.getPatchZhiLiJianCeID() != null) {
            patchVo.setZhilijiance(zhiLiJianCeService.selectZhilijianceByZhiLiJianCeID(patch.getPatchZhiLiJianCeID()));
        }
        return success(patchVo);
    }
}
